<?php
/* Begin Widget Class */
class DT_clients extends WP_Widget {

	/* Widget setup  */
	function DT_clients() {
		/* Widget settings. */
		$widget_ops = array(
            'classname'     => 'dt_clients',
            'description'   => __('Here some of our clients', LANGUAGE_ZONE)
        );

		/* Widget control settings. */
		$control_ops = array(
            'width'     => 250,
            'height'    => 250,
            'id_base'   => 'dt_clients'
        );

		/* Create the widget. */
		$this->WP_Widget(
            'dt_clients',
            __( THEME_TITLE.' Our Clients', LANGUAGE_ZONE ),
            $widget_ops,
            $control_ops
        );
	}

	/* Display the widget  */
	function widget( $args, $instance ) {
		extract( $args );
        
        $ppp = $instance['ppp'];
        if( !$ppp ) {
            $ppp = -1;
        }
        
		// get testimonials all with status publish
        $dt_query = new Wp_Query(
            array(
                'post_type'         => 'dt_our_clients',
                'posts_per_page'    => $ppp,
                'post_status'       => 'publish',
                'orderby'           => $instance['orderby']
            )
        );
        $dt_posts = $dt_query->posts;
        
        /* Our variables from the widget settings. */
		$title = apply_filters('widget_title', $instance['title'] );
       
        echo $before_widget ;
        
        if( $title ) {
            echo $before_title . $title . $after_title;
        }
        
        echo '<ul class="our_clients">';
        
        if( $dt_posts ) {
            for( $i = 0; isset($dt_posts[$i]); $i++ ) {
                $img = $content = '';
                
                $defaults = array(
                    'link'    => ''
                );
                $data = get_post_meta( $dt_posts[$i]->ID, 'our_clients', true );
                $options = wp_parse_args( $data, $defaults );
                
                $defaults = array(
                    'quality'   => 100,
                    'zc'        => true,
                    'zc_align'  => 'c'
                );
                $opts = get_post_meta( $dt_posts[$i]->ID, 'dt_post_thumb_opts_options', true );
                $opts = wp_parse_args( $opts, $defaults );
                
                // image
                if( has_post_thumbnail($dt_posts[$i]->ID) ) {
                    
                    $args = array(
                        'width'     => 140,
                        'height'    => 70,
                        'upscale'   => $opts['zc'],
                        'post_id'   => $dt_posts[$i]->ID,
                        'quality'   => $opts['quality'],
                        'zc_align'  => $opts['zc_align']
                    );
                    
                    if( isset($id) && 'bottom-widget-area' == $id ) {
                        $args['width'] = 96;
                        $args['height'] = 64;
                    }
                    
                    $thumb = dt_get_thumbnail( $args );
                }
                
                $dt_link = '%s';
                if( !empty($options['link']) ) {
                    $dt_link = '<a href="' . $options['link'] . '">%s</a>'; 
                }
                
                $content = sprintf(
                    $dt_link,
                    '<img src="'. $thumb['thumnail_img']. '"/>'
                );
                
                echo '<li>'. "\n". $content. "\n". '</li>'. "\n";
            }          
        }
        
        echo '</ul>';
       
        echo $after_widget;
	}

	/* Update the widget settings  */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['ppp'] = $new_instance['ppp'];
        $instance['orderby'] = $new_instance['orderby'];
        
		return $instance;
	}

	/**
	 * Displays the widget settings controls on the widget panel.
	 * Make use of the get_field_id() and get_field_name() function
	 * when creating your form elements. This handles the confusing stuff.
	 */
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 
			'title'             => '',
            'ppp'               => '',
            'orderby'           => 'date'
        );

		$instance = wp_parse_args( (array) $instance, $defaults );
    ?>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', LANGUAGE_ZONE); ?></label>
			<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:85%;" />
		</p>
        
        <p>
			<label for="<?php echo $this->get_field_id( 'ppp' ); ?>"><?php _e('Show amount:', LANGUAGE_ZONE); ?></label>
			<input type="text" id="<?php echo $this->get_field_id( 'ppp' ); ?>" name="<?php echo $this->get_field_name( 'ppp' ); ?>" value="<?php echo $instance['ppp']; ?>" style="width:85%;" />
		</p>
        
        <p>
			<label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e('Order by Date:', LANGUAGE_ZONE); ?></label>
			
            <input type="radio" id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>" <?php checked($instance['orderby'], 'date'); ?> value="date"/>
            
            <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e('Random Order:', LANGUAGE_ZONE); ?></label>
			
            <input type="radio" id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>" <?php checked($instance['orderby'], 'rand'); ?> value="rand"/>
		</p>
				
		<div style="clear: both;"></div>
	<?php
	}
}

function dt_our_clients_register() {
	register_widget( 'DT_clients' );
}

add_action( 'widgets_init', 'dt_our_clients_register' );
